package com.google.code.mamule.mail;

import com.google.code.mamule.user.User;
import com.google.common.base.Objects;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Created with IntelliJ IDEA. User: borck_000 ; Date: 12/22/13 ; Time: 8:08 PM
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@Entity
public class Mail {

  @Id
  @GeneratedValue
  private Long id;

  @ManyToOne(fetch = FetchType.EAGER)
  private User owner;
  @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinTable(name = "MAIL_RECEIVER")
  private Set<Mailable> receiver;

  private String text;

  @Enumerated(EnumType.STRING)
  private MailStatus status;
  @ElementCollection(fetch = FetchType.EAGER)
  private Set<MailOption> options;

  // @ManyToOne(fetch = FetchType.EAGER)
  // private Order order;

  public Mail() {
  } /* JAXB */

  public Mail(final User owner, final Set<Mailable> receiver, final String text,
              final MailStatus status, final Set<MailOption> options) {
    this.owner = owner;
    this.receiver = receiver;
    this.text = text;
    this.status = status;
    this.options = options;
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(this)
        .add("id", id)
        .add("owner", owner)
        .add("receiver", receiver)
        .add("text", text)
        .add("status", status)
        .add("options", options)
        .toString();
  }

  // JAXB DOC GETTER

  public Mailable getSender() {
    return owner.getIdentity();
  }

  public void setSender(Mailable sender) {
    // ignored
  }

  public String getOwnerName() {
    return owner.getUsername();
  }

  public void setOwnerName(String name) {
    // ignored
  }

  // ENDOF DOC GETTER

  public Long getId() {
    return id;
  }

  public void setId(final Long id) {
    this.id = id;
  }

  @XmlTransient
  public User getOwner() {
    return owner;
  }

  public void setOwner(final User owner) {
    this.owner = owner;
  }

  public Set<Mailable> getReceiver() {
    return receiver;
  }

  public void setReceiver(final Set<Mailable> receiver) {
    this.receiver = receiver;
  }

  public String getText() {
    return text;
  }

  public void setText(final String text) {
    this.text = text;
  }

  public MailStatus getStatus() {
    return status;
  }

  public void setStatus(final MailStatus status) {
    this.status = status;
  }

  public Set<MailOption> getOptions() {
    return options;
  }

  public void setOptions(final Set<MailOption> options) {
    this.options = options;
  }
}

